package com.itheima.ydd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.dto.SysUserInfo;
import com.itheima.ydd.entity.User;
import com.itheima.ydd.mapper.UserMapper;
import com.itheima.ydd.service.RoleService;
import com.itheima.ydd.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @since 2022-02-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private RoleService roleService;


    /**
     *分页条件查询 用户管理
     * @param page 页码
     * @param limit 页面条目数
     * @param user 查询条件,用户名,状态,手机号
     * @return 分页查询对象
     */
    @Override
    public PageResult getList(Long page, Long limit, User user) {
        //获取分页查询对象
        Page<User> userPage=new Page<>();
        LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
        lqw.eq(user.getStatus()!=null,User::getStatus,user.getStatus())
                .like(user.getLogin_name()!=null,User::getLogin_name,user.getLogin_name())
                .like(user.getPhone()!=null,User::getPhone,user.getPhone());
        this.page(userPage,lqw);
        //获取用户数据,并查询该用户的角色集合
       List<SysUserInfo> userDtos=userPage.getRecords().stream().map(sysUser->{
           SysUserInfo userDto=new SysUserInfo();
            BeanUtils.copyProperties(sysUser,userDto);
            userDto.setRolename(roleService.roleNameList(sysUser.getRole_id().substring(2,sysUser.getRole_id().length()-2)));
           System.out.println(userDto.getRolename());
            return userDto;
        }).collect(Collectors.toList());
        System.out.println(userDtos);
        return PageResult.init(userPage.getTotal(),limit,page,userDtos);
    }
}
